package com.service.impl;

import com.domain.entity.Assort;
import com.exception.ServiceException;
import com.mapper.AssortMapper;
import com.service.AssortService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author caishenao
* @description 针对表【assort】的数据库操作Service实现
* @createDate 2023-10-12 20:12:21
*/
@Service
@Slf4j
public class AssortServiceImpl implements AssortService{

    @Autowired
    private AssortMapper assertionMapper;

    /**
     * 新增商品分类
     *
     * @param name 分类名称
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAssort(String name) {
        // 1. 判断分类名称是否存在
        // 去除"字符
        name = name.replace("\"","");
        Assort assort = assertionMapper.getByName(name);

        if (assort != null) {
            log.error("名字为:{}的分类信息已存在，无需新增", name);
            throw new ServiceException("分类信息已存在，无需新增");
        }

        // 2. 转换为商品分类信息
        assort = new Assort();
        assort.setName(name);
        assort.setCreateTime(new Date());

        // 3. 新增进数据库
        assertionMapper.insert(assort);
    }

    /**
     * 查询所有分类信息
     *
     * @return 分类信息集合
     */
    @Override
    public List<Assort> list() {
        return assertionMapper.list();
    }

    /**
     * 根据id删除分类信息
     *
     * @param id 分类id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Integer id) {
        // 1. 判断分类信息是否存在
        Assort assort = assertionMapper.getById(id);
    }
}




